%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%THIS FILE ESTIMATES THE ORDERED PROBIT MODEL IN 
%"Evidence of Neighborhood Effects from Moving to Opportunity: 
%     LATEs of Neighborhood Quality"
%by Dionissi Aliprantis and Francisca G.-C. Richter
%SEE THE INCLUDED readme.txt FILE FOR AN EXPLANATION OF THIS FILE 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%IMPORTING DATA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=importdata('C:\MTO_1\matlab\matlab_data.txt');
data( any(data==-9,2), : ) = [];
options = optimset('TolFun', 1e-8, 'TolX', 1e-8, 'GradObj', 'on', 'Display', 'iter');   %SHOW ITERATIONS


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%LL_MTO_simplest - AN ORDERED PROBIT MODEL WITHOUT INSTRUMENTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x0=zeros(17,1);
x0(5)=0.2;
x0(7)=-0.2;
x0(8)=-0.3;
for i=9:17
    x0(i)=-1+.1*(i-8);
end
[x,fval, output, exitflag] = fminsearch('LL_MTO_simplest',x0,options)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%LL_MTO_simple - AN ORDERED PROBIT MODEL WITH INSTRUMENTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x0=zeros(43,1);
x0(5)=0.2;
x0(7)=-0.2;
x0(8)=-0.3;
for i=9:17
    x0(i)=-1+.3*(i-8);
end
options = optimset('TolFun', 1e-8, 'TolX', 1e-8, 'GradObj', 'on', 'Display', 'iter');   %SHOW ITERATIONS
[x,fval, output, exitflag] = fminsearch('LL_MTO_simple',x0,options)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%LL_MTO - ACTUAL MODEL
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

data=importdata('C:\MTO_1\matlab\matlab_data.txt');
data( any(data==-9,2), : ) = [];
options = optimset('TolFun', 1e-8, 'TolX', 1e-8, 'GradObj', 'off', 'Display', 'iter');   %SHOW ITERATIONS
options = statset('TolX',1e-4);

%%%%%%%%%%%%%%%%%%%%%%
%SETTING INITIAL VALUE
%%%%%%%%%%%%%%%%%%%%%%
x0=zeros(63,1);

%BETAS
x0(1)=0.09;
x0(2)=0.12;
x0(3)=0.04;
x0(4)=0.12;
x0(5)=0.40;
x0(6)=0.15;
x0(7)=-0.43;
x0(8)=-0.51;

%CUTPOINTS
x0(9)=-0.95;
x0(10)=-0.45;
x0(11)=-0.07;
x0(12)=0.30;
x0(13)=0.60;
x0(14)=0.98;
x0(15)=1.31;
x0(16)=1.66;
x0(17)=2.30; 

%gamma8_j
x0(18)=0.75;
x0(19)=0.55;
x0(20)=0.55;
x0(21)=0.55;
x0(22)=0.55;
x0(23)=0.60;
x0(24)=0.52;
x0(25)=0.40;
x0(26)=0.40;

x0(27)=0.0;
x0(28)=-0.18;
x0(29)=0.27;
x0(30)=0.26;

%gamma1_j
x0(31)=1.06;
x0(32)=1.06;
x0(33)=1.06;
x0(34)=1.17;
x0(35)=1.17;
x0(36)=1.35;
x0(37)=1.35;
x0(38)=1.20;
x0(39)=0.90;

x0(40)=-0.12;
x0(41)=-0.15;
x0(42)=0.24;
x0(43)=0.14;

%SECTION 8 TYPE COEFFICIENTS FROM STATA ESTIMATES OF 
%probit svy_cmove $X [pw= wt_totsvy] if exp==1
x0(44)=0.53;
x0(45)=0.15;
x0(46)=0.07;
x0(47)=-0.03;
x0(48)=-0.43;
x0(49)=-0.07;
x0(50)=0.38;
x0(51)=-0.60;

x0(52)=0.07;

%EXP TYPE COEFFICIENTS FROM STATA ESTIMATES OF 
%probit  svy_cmove $X [pw= wt_totsvy] if exp==2
x0(53)=0.41;
x0(54)=0.02;
x0(55)=0.12;
x0(56)=-0.08;
x0(57)=0.00;
x0(58)=-0.56;
x0(59)=0.36;
x0(60)=-0.15;

x0(61)=-0.17;
   
%RHOs
x0(62)=0;
x0(63)=0;

[x,fval, output, exitflag] = fminsearch('LL_MTO2',x0,options)
xhat=x;
save xhat.txt x -ascii




